package com.gisocn.onemap.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/* loaded from: classes2.dex */
public class ImageUtils {
    public static final int CROP_IMAGE = 5003;
    public static final int GET_IMAGE_BY_CAMERA = 5001;
    public static final int GET_IMAGE_FROM_PHONE = 5002;
    public static Uri cropImageUri;
    public static Uri imageUriFromCamera;

    public static String SaveBitmap(Bitmap bitmap, String str, String str2) {
        File file = new File(str2);
        if (!file.exists()) {
            file.mkdirs();
        }
        String str3 = null;
        try {
            str3 = str2 + str;
            FileOutputStream fileOutputStream = new FileOutputStream(str3);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            return str3;
        } catch (Exception e) {
            e.printStackTrace();
            return str3;
        }
    }

    public static void openCameraImage(Activity activity) {
        imageUriFromCamera = createImagePathUri(activity);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra("output", imageUriFromCamera);
        activity.startActivityForResult(intent, 5001);
    }

    public static void openLocalImage(Activity activity) {
        Intent intent = new Intent("android.intent.action.PICK", MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        activity.startActivityForResult(intent, 5002);
    }

    public static void cropImage(Activity activity, Uri uri, int i, int i2) {
        cropImageUri = createImagePathUri(activity);
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", -1);
        intent.putExtra("outputY", -1);
        intent.putExtra("output", cropImageUri);
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", false);
        activity.startActivityForResult(intent, 5003);
    }

    private static Uri createImagePathUri(Context context) {
        Uri insert;
        String externalStorageState = Environment.getExternalStorageState();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);
        long currentTimeMillis = System.currentTimeMillis();
        String format = simpleDateFormat.format(new Date(currentTimeMillis));
        ContentValues contentValues = new ContentValues(3);
        contentValues.put("_display_name", format);
        contentValues.put("datetaken", Long.valueOf(currentTimeMillis));
        contentValues.put("mime_type", "image/jpg");
        if (externalStorageState.equals("mounted")) {
            insert = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        } else {
            insert = context.getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, contentValues);
        }
        Log.e("生成的照片输出路径", "生成的照片输出路径：" + insert.toString());
        return insert;
    }

    public static File getFileByUri(Activity activity, Uri uri) {
        int i = 0;
        if ("file".equals(uri.getScheme())) {
            String encodedPath = uri.getEncodedPath();
            if (encodedPath != null) {
                encodedPath = Uri.decode(encodedPath);
                ContentResolver contentResolver = activity.getContentResolver();
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("(");
                stringBuffer.append("_data");
                stringBuffer.append("=");
                stringBuffer.append("'" + encodedPath + "'");
                stringBuffer.append(")");
                Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data"}, stringBuffer.toString(), null, null);
                query.moveToFirst();
                while (!query.isAfterLast()) {
                    i = query.getInt(query.getColumnIndex("_id"));
                    encodedPath = query.getString(query.getColumnIndex("_data"));
                    query.moveToNext();
                }
                query.close();
                if (i != 0) {
                    Uri parse = Uri.parse("content://media/external/images/media/" + i);
                    PrintStream printStream = System.out;
                    printStream.println("temp uri is :" + parse);
                }
            }
            if (encodedPath != null) {
                return new File(encodedPath);
            }
        } else if ("content".equals(uri.getScheme())) {
            Cursor query2 = activity.getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
            String string = query2.moveToFirst() ? query2.getString(query2.getColumnIndexOrThrow("_data")) : null;
            query2.close();
            return new File(string);
        } else {
            Log.i("", "Uri Scheme:" + uri.getScheme());
        }
        return null;
    }
}
